'use strict';
const db = uniCloud.database()

exports.main = async (event, context) => {
  try {
    const collection = db.collection('species')
    const { category, page = 1, pageSize = 10 } = event
    
    // 构建查询条件
    let query = collection
    if (category) {
      query = query.where({
        category: category
      })
    }
    
    // 获取总数
    const countResult = await query.count()
    const total = countResult.total
    
    // 获取分页数据
    const result = await query
      .skip((page - 1) * pageSize)
      .limit(pageSize)
      .get()
    
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: result.data,
        total,
        page,
        pageSize
      }
    }
  } catch (error) {
    return {
      code: 500,
      message: '获取失败：' + error.message
    }
  }
} 